package com.itheima.tlais_web_management.controller;

import com.itheima.tlais_web_management.pojo.Result;
import com.itheima.tlais_web_management.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

//    @PostMapping("/upload")
//    public Result upload(String username,Integer age,MultipartFile image) throws Exception {
//        log.info("文件上传：{},{},{}",username,age,image);
//
//        //文件名
//        String filename = image.getOriginalFilename();
//
//        /**
//         * 先获取点的位置
//         * 然后获取点后面的扩展名
//         * 组合一个新名字
//         */
//        int index = filename.lastIndexOf(".");
//        String substring = filename.substring(index);
//        String s = UUID.randomUUID().toString() + substring;
//        log.info("新的文件名{}",s);
//
//
//        image.transferTo(new File("D:\\images\\" + s));
//
//        return Result.success();
//    }
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws Exception {
        log.info("文件上传:{}",image.getOriginalFilename());

        String url = aliOSSUtils.upload(image);
        log.info("文件上传完成，返回的url是:{}",url);

        return Result.success(url);
    }

}























